#include "IMUser.h"
#include "IMUserManager.h"

IMUser::IMUser() : uid_(0), status_(online_type_offline)
{
    teams_id_[IM_TEAM_NAME_DEFAULT] = 0;
    teams_id_[IM_TEAM_NAME_BLACK] = 255;
    teams_name_[0] = IM_TEAM_NAME_DEFAULT;
    teams_name_[255] = IM_TEAM_NAME_BLACK;
}

IMUser::IMUser(const userid_t &uid, const std::string &username, const std::string &nickname, const online_type &status, const IMSessionPtr &session)
    : uid_(uid), username_(username), nickname_(nickname), status_(status), session_(session)
{
    teams_id_[IM_TEAM_NAME_DEFAULT] = 0;
    teams_id_[IM_TEAM_NAME_BLACK] = 255;
    teams_name_[0] = IM_TEAM_NAME_DEFAULT;
    teams_name_[255] = IM_TEAM_NAME_BLACK;
}

void IMUser::send(const msg_type &t, const char *data, const size_t &len)
{
    this->session_->send(t, data, len);
}

void IMUser::iniTeam(boost::property_tree::ptree &pt)
{
    for (const auto &pair : pt)
    {
        teams_id_[pair.first] = pair.second.get_value<uint8_t>();
        teams_name_[pair.second.get_value<uint8_t>()] = pair.first;
    }
}

bool IMUser::addTeam(const std::string &name)
{
    // 如果已经存在该分组，则返回false
    if (teams_id_.find(name) != teams_id_.end())
    {
        return false;
    }
    // 如果分组数量已经达到上限，则返回false
    if (teams_id_.size() >= 256)
    {
        return false;
    }
    uint8_t id = 1;
    while (teams_name_.find(id) != teams_name_.end()) // 寻找还未使用的id号码
    {
        id++;
    }

    teams_id_[name] = id;
    teams_name_[id] = name;
    // 写入数据库
    return true;
}

bool IMUser::delTeam(const std::string &name)
{
    auto it = teams_id_.find(name);
    if (it != teams_id_.end())
    {
        // 先判断该分组是否为默认分组和黑名单
        if (it->second == 0 || it->second == 255)
        {
            return false;
        }
        teams_id_.erase(it);
    }
    // 删除分组后，需要将该分组下的好友移动到默认分组
    for (auto &friend_ : friends_)
    {
        if (friend_.teamId == it->second)
        {
            friend_.teamName = teams_name_[0];
            friend_.teamId = 0;
        }
    }
    // 写入数据库
    return true;
}

bool IMUser::isFriend(const userid_t &uid)
{
    for (auto &friend_ : friends_)
    {
        if (friend_.user->uid_ == uid)
        {
            return true;
        }
    }
    return false;
}

bool IMUser::isFriend(const std::string &username)
{
    for (auto &friend_ : friends_)
    {
        if (friend_.user->username_ == username)
        {
            return true;
        }
    }
    return false;
}

bool IMUser::moveFriend(const userid_t &uid, const std::string &team_name)
{
    // 先判断该分组是否存在
    auto it = teams_id_.find(team_name);
    if (it == teams_id_.end())
    {
        // return false;
        // 如果分组不存在，则创建该分组
        if (!addTeam(team_name))
        {
            return false;
        }
    }
    // 再判断该好友是否存在
    for (auto &friend_ : friends_)
    {
        if (friend_.user->uid_ == uid)
        {
            friend_.teamName = team_name;
            friend_.teamId = it->second;
            return true;
        }
    }
    // 写入数据库
    return false;
}

void IMUser::addFriend(const userid_t &uid)
{
    // 先判断该好友是否已经存在
    for (auto &friend_ : friends_)
    {
        if (friend_.user->uid_ == uid)
        {
            return;
        }
    }
    // 判断好友数量是否达到上限
    if (friends_.size() >= IM_FRIEND_MAX_SIZE)
    {
        return;
    }
    // 添加好友
    auto user = IMUserManager::getInstance()->FindUser(uid);
    if (user != nullptr)
    {
        Friend friend_;
        friend_.teamName = teams_name_[0];
        friend_.teamId = 0;
        friend_.user = user;
        friends_.push_back(friend_);
    }
    // 写入数据库
    return;
}

void IMUser::addFriend(const userid_t &uid, const std::string &team_name)
{
    // 先判断该好友是否已经存在
    for (auto &friend_ : friends_)
    {
        if (friend_.user->uid_ == uid)
        {
            return;
        }
    }
    // 判断好友数量是否达到上限
    if (friends_.size() >= IM_FRIEND_MAX_SIZE)
    {
        return;
    }
    // 添加好友
    auto user = IMUserManager::getInstance()->FindUser(uid);
    // 先判断该分组是否存在
    auto it = teams_id_.find(team_name);
    if (it == teams_id_.end())
    {
        addTeam(team_name);
    }
    it = teams_id_.find(team_name);
    if (user != nullptr)
    {
        Friend friend_;
        friend_.teamName = team_name;
        friend_.teamId = it->second;
        friend_.user = user;
        friends_.push_back(friend_);
    }
    // 写入数据库
}

void IMUser::removeFriend(const userid_t &uid)
{
    for (auto it = friends_.begin(); it != friends_.end(); ++it)
    {
        if ((*it).user->uid_ == uid)
        {
            friends_.erase(it);
            return;
        }
    }
}

void IMUser::addGroup(const groupid_t &gid)
{
    for (auto &group : groups_)
    {
        if (group == gid)
        {
            return;
        }
    }
    groups_.push_back(gid);
    // 写入数据库
}

void IMUser::removeGroup(const groupid_t &gid)
{
    for (auto it = groups_.begin(); it != groups_.end(); ++it)
    {
        if ((*it) == gid)
        {
            groups_.erase(it);
            return;
        }
    }
    // 写入数据库
}

void IMUser::dbAddGroup(const groupid_t &gid)
{
    for (auto &group : groups_)
    {
        if (group == gid)
        {
            return;
        }
    }
    groups_.push_back(gid);
}
